package cn.frh.org.service.impl;

import cn.frh.basic.exception.BusinessException;
import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.org.domain.Employee;
import cn.frh.org.domain.Shop;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.org.mapper.ShopMapper;
import cn.frh.org.service.IEmployeeService;
import cn.frh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    //基础的继承
    //扩展的自己写

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        //一：校验
         //1.空校验
        if(StringUtils.isEmpty(shop)){
            throw new BusinessException("参数不能为空");
        }
        //2.两次密码是否一致
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致！");
        }
        //3.判断是否注册过
        Employee tmp = employeeMapper.loadByUserName(admin.getUsername());
        if (tmp != null){
            throw new BusinessException("该用户已存在");
        }
      //校验通过 先把emp的信息保存，在保存shop 返回shop的id 修改emp的shop_id
        //保存emp
        //employeeMapper.save(admin);
        employeeService.add(admin);
        //在保存shop 返回shop的id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        //修改emp的shopid
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
